package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.AddressBookDTO;
import com.sky.entity.AddressBook;
import com.sky.exception.AddressBookBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.netty.handler.codec.MessageAggregationException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: AddressBookServiceImpl
 *
 * @AUTHOR: zhang
 * DATE: 2023/12/1
 * DESCRIPTION :
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookMapper;
    /**
     * 查询地址列表
     * @return
     */
    @Override
    public Result<List<AddressBook>> selectAllAddressBook() {
        //查询当前用户的地址列表
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        List<AddressBook> addressBookList = addressBookMapper.selectAddressBook(addressBook);
        return Result.success(addressBookList);
    }

    /**
     * 新增地址
     * @param addressBookDTO
     */
    @Override
    public void inseertAddressBook(AddressBookDTO addressBookDTO) {
        //拷贝属性
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDTO,addressBook);
        //补齐属性 userId is_default create_time update_time
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        //入库
        addressBookMapper.insertAddressBook(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        //根据当前用户先把所有的默认地址修改为0(未设置)
        Long userId = BaseContext.getCurrentId();
        AddressBook book = new AddressBook();
        book.setUserId(userId);
        addressBookMapper.updateISDefaultByuserId(book);
        //再根据地址id设置默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.upadte(addressBook);
    }

    /**
     * 根据id查询地址信息
     * @param id
     * @return
     */
    @Override
    public AddressBook selectAddressBookById(Long id) {
       return addressBookMapper.selectAddressBookById(id);
    }

    /**
     * 根据id修改地址
     * @param addressBookDTO
     */
    @Override
    public void updateAddressBook(AddressBookDTO addressBookDTO) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDTO,addressBook);
        addressBookMapper.upadte(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deleteAddressBook(Long id) {
        addressBookMapper.deleteAddressById(id);
    }

    /**
     * 获取默认地址
     */
    @Override
    public AddressBook getDefaultAddressBook() {
        return addressBookMapper.getDefaultAddressBook();
    }
}
